package com.proj.android.canyondefense.core.math;

public class OverlapTester {

	public static boolean overlapCircles(Circle c1, Circle c2) {
        float distance = c1.center.distSquared(c2.center);
        float radiusSum = c1.radius + c2.radius;
        return distance <= radiusSum * radiusSum;
    }
    
    public static boolean overlapRectangles(Rectangle rect1, Rectangle rect2) {
        if(rect1.pos.x < rect2.pos.x + rect2.width &&
           rect1.pos.x + rect1.width > rect2.pos.x &&
           rect1.pos.y < rect2.pos.y + rect2.height &&
           rect1.pos.y + rect1.height > rect2.pos.y) {
            return true;
        } else {
            return false;
        }
    }
    
    public static boolean overlapCircleRectangle(Circle c, Rectangle rect) {
        float closestX = c.center.x;
        float closestY = c.center.y;
        
        if(c.center.x < rect.pos.x) {
            closestX = rect.pos.x; 
        } else if(c.center.x > rect.pos.x + rect.width) {
            closestX = rect.pos.x + rect.width;
        }
          
        if(c.center.y < rect.pos.y) {
            closestY = rect.pos.y;
        } else if(c.center.y > rect.pos.y + rect.height) {
            closestY = rect.pos.y + rect.height;
        }
        
        return c.center.distSquared(closestX, closestY) < c.radius * c.radius;           
    }
    
    public static boolean pointInCircle(Circle c, Vector2 p) {
        return c.center.distSquared(p) < c.radius * c.radius;
    }
    
    public static boolean pointInCircle(Circle c, float x, float y) {
        return c.center.distSquared(x, y) < c.radius * c.radius;
    }
    
    public static boolean pointInRectangle(Rectangle rect, Vector2 p) {
        return rect.pos.x <= p.x && rect.pos.x + rect.width >= p.x &&
               rect.pos.y <= p.y && rect.pos.y + rect.height >= p.y;
    }
    
    public static boolean pointInRectangle(Rectangle rect, float x, float y) {
        return rect.pos.x <= x && rect.pos.x + rect.width >= x &&
               rect.pos.y <= y && rect.pos.y + rect.height >= y;
    }

}
